const {nowTime,emptyData,fileterTime} = require("../utils/misc")
const ObjectId = require('mongodb').ObjectId
const router = require('koa-router')();
const mongo = require('../compents/mongo')
const koaMulter = require('@koa/multer')
const {postLogData} =require("../utils/misc")
router.prefix('/seckill')


router.get('/list', async ctx => {
    postLogData("请求秒杀")
    let { page,startTime,endTime,name,campus } = ctx.query
    let where={name,campus},fileterData={}
    // 判断是否传值 未传值直接删除对象
    name?"":delete where.name
    campus?"":delete where.campus
    let res = await mongo.find('seckill', {where,limit:10000})
    startTime?(endTime?fileterData=fileterTime(res,{startTime,endTime,page}):""):""
    let total = res.length
    if(JSON.stringify(fileterData)!="{}"){
        res=fileterData.arr
        total=fileterData.total
    }
    if (res) {
        ctx.body = { data: res, code: 20000, total, message: "数据请求成功" }
    } else {
        ctx.body = {data:res, code: 50001, message: "数据请求失败" }
    }
})

router.post('/save', async ctx => {
    postLogData("新增秒杀")
    let stuData = ctx.request.body
    stuData= emptyData(stuData,["name","campus","avatar","acCode","acStartTime","acEndTime","state"])
    const res = await mongo.insertOne('seckill', { ...stuData,uploadTime:nowTime()})
    if (res.insertedId) {
        ctx.body = { data: res, code: 20000, message: "数据插入成功" };
    } else {
        ctx.body = { data: res, code: 50002, message: "数据插入失败" };
    }
})
// 图片上传
var storage = koaMulter.diskStorage({  //配置文件上传的方式   memoryStorage 内存的意思 把图片上传到内存里   diskStorage硬盘的意思 所有上传的文件都放到硬盘里
    //文件保存路径
    destination: (req, file, cb) => {
        cb(null, './uploads')  //注意路径必须存在
    },
    //修改文件名称
    filename: (req, file, cb) => {
        const ext = file.originalname.split(".").pop()
        cb(null, 'seckill-' + Date.now() + "." + ext)
    }
})
var upload = koaMulter({ storage })
router.post('/save-avatar', upload.single('avatar'), async ctx => {
    console.log(ctx);
    if(ctx.file.filename){
        ctx.body = { data: ctx.file.filename, msg: "文件上传成功", code: 20000 }
    }else{
        ctx.body = { msg: "文件上传失败", code: 50003 }
    }
})

// 修改数据库
router.post('/change', async ctx => {
    postLogData("修改秒杀")
    const { _id } = ctx.request.body
    const update = ctx.request.body
    delete update._id
    const res = await mongo.updateOne('seckill', { where: { _id: ObjectId(_id) }, update })
    if (res.modifiedCount > 0) {
        ctx.body = { data: res, code: 20000, message: "数据修改成功" };
    } else {
        ctx.body = { data: res, code: 50004, message: "数据修改失败" };
    }
})

// 删除数据
// 删除单挑数据
router.get('/delOne', async ctx => {
    postLogData("单删秒杀")
    const { _id } = ctx.query
    const res = await mongo.deleteOne('seckill', { _id: ObjectId(_id) })
    if (res.deletedCount > 0) {
        ctx.body = { data: res, code: 20000, message: "数据删除成功" };
    } else {
        ctx.body = { data: res, code: 50005, message: "数据删除失败" };
    }
})
// 删除多条数据  
router.get('/delMany', async ctx => {
    postLogData("多删秒杀")
    let { ids } = ctx.query
    ids = JSON.parse(ids)
    const res = await mongo.deleteMany('seckill', ids)
    if (res.deletedCount > 0) {
        ctx.body = { data: res, code: 20000, message: "数据批量删除成功" };
    } else {
        ctx.body = { data: res, code: 50006, message: "数据批量删除失败" };
    }
})
module.exports = router 